package net.valerijtimakov.homefinances.dao.storages.finances.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

import net.valerijtimakov.homefinances.domain.common.DataItem;
import net.valerijtimakov.homefinances.domain.finances.CostsItem;
import net.valerijtimakov.homefinances.exceptions.AppExceptionWrongState;
import net.valerijtimakov.homefinances.exceptions.AppPersProviderException;
import net.valerijtimakov.homefinances.exceptions.AppPersistentException;
import net.valerijtimakov.homefinances.exceptions.wrongarg.AppExceptionNullArgument;
import net.valerijtimakov.homefinances.dao.storages.common.mysql.OperationItemsStorage;
import net.valerijtimakov.homefinances.dao.storages.common.mysql.query_builders.condition.WhereBuilder;
import net.valerijtimakov.homefinances.dao.storages.finances.ICostsStorage;

public class CostsStorage extends OperationItemsStorage implements ICostsStorage {
	
	protected String costsFieldName = "costs";

	public CostsStorage(IMySqlProvider provider) throws AppExceptionWrongState {
		super(provider);
	}

	public Map<Long, CostsItem> filterCosts(long[] accountsIds, long[] cathegoriesIds, long[] currenciesIds, 
		Date addDateFrom, Date addDateTo, final float costFrom, final float costTo, String namePattern) 
		throws AppPersProviderException, AppExceptionWrongState 
	{
		return super.filterCosts(accountsIds, cathegoriesIds, currenciesIds, addDateFrom, addDateTo, namePattern, 
			new FiltersAdditor() {
				public void addFilters(WhereBuilder whereBuilder) {
					whereBuilder.addIntervalCondBuilder(costsFieldName, costFrom, costTo);
				}
			});
	}

	public Map<Long, CostsItem> getAccountsCosts(long... accountId) 
		throws AppPersProviderException, AppExceptionWrongState 
	{
		return getAccountOperations(accountId);
	}

	public Map<Long, CostsItem> getCosts(long... id) throws AppPersProviderException, AppExceptionWrongState {
		return getItemsByIds(id);
	}

	public void addCost(CostsItem item) throws AppExceptionNullArgument, AppExceptionWrongState, AppPersistentException {
		addItem(item, "Не задан экземпляр расхода!", "Ошибка добавления расхода!");
	}

	public void removeCost(long id) throws AppPersProviderException, AppExceptionWrongState {
		delete(id);
	}

	public void updateCost(CostsItem item) throws AppPersProviderException, AppExceptionWrongState {
		update(item);
	}

	@Override
	protected String getTableName() {
		return "costs";
	}

	@Override
	protected DataItem createTempItem() {
		return new CostsItem();
	}

	@Override
	protected <T extends DataItem> void parse(ResultSet rs, T item) throws SQLException {
		super.parse(rs, item);
		CostsItem cItem = (CostsItem)item;
		cItem.setCost(rs.getFloat(costsFieldName));
	}
	
	@Override
	protected void initSelect() {
		super.initSelect();
		selectQuery.addField(costsFieldName);
	}

	@Override
	protected <T extends DataItem> void addData(T item) {
		super.addData(item);
		CostsItem cItem = (CostsItem)item;
		insertQuery.setFieldValue(costsFieldName, cItem.getCost());
	}

	@Override
	protected <T extends DataItem> void setUpdatedFields(T item) {
		super.setUpdatedFields(item);
		CostsItem cItem = (CostsItem)item;
		updateQuery.setFieldValue(costsFieldName, cItem.getCost());
	}
}
